\defmodule {BakerTransformedStream}

This container class permits one to apply the baker's transformation to
the output of any \class{RandomStream}.  
It transforms each $u \in [0,1]$ into $2u$ if $u \le 1/2$ and $2(1-u)$
if $u > 1/2$.
The \method{nextDouble}{} method will return the result of this transformation
and the other \texttt{next...} methods are affected accordingly.
Any instance of this class contains a \class{RandomStream} called its
\emph{base stream}, used to generate its numbers and to which the
transformation is applied. 
Any call to one of the \texttt{next...}
methods of this class will modify the state of the base stream. 

The baker transformation is often applied when the \class{RandomStream}
is actually an iterator over a point set used for quasi-Monte Carlo
integration (see the \texttt{hups} package).


%%%%%%%%%%%%%%%%%%%%%%%%%%%
\bigskip\hrule

\begin{code}
\begin{hide}
/*
 * Class:        BakerTransformedStream
 * Description:  container class permits one to apply the baker's 
                 transformation to the output of any RandomStream
 * Environment:  Java
 * Software:     SSJ 
 * Copyright (C) 2001  Pierre L'Ecuyer and Universite de Montreal
 * Organization: DIRO, Universite de Montreal
 * @author       
 * @since

 * SSJ is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License (GPL) as published by the
 * Free Software Foundation, either version 3 of the License, or
 * any later version.

 * SSJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * A copy of the GNU General Public License is available at
   <a href="http://www.gnu.org/licenses">GPL licence site</a>.
 */
\end{hide}
package umontreal.iro.lecuyer.rng; 

public class BakerTransformedStream implements RandomStream \begin{hide} {

   // The base stream.
   private RandomStream st;

\end{hide}
\end{code}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection* {Constructors}

\begin{code}

   public BakerTransformedStream (RandomStream stream) \begin{hide} {
      st = stream;
   } \end{hide}
\end{code}
\begin{tabb} Constructs a new baker transformed stream, using the random
  numbers from the base stream \texttt{stream}.
\end{tabb}

%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsubsection* {Methods}
\begin{code}\begin{hide}
   public void resetStartStream() {
      st.resetStartStream();
   }

   public void resetStartSubstream() {
      st.resetStartSubstream();
   }

   public void resetNextSubstream() {
      st.resetNextSubstream();
   }\end{hide}

   public String toString() \begin{hide} {
      return "Baker transformation of " + st.toString();
   }\end{hide}
\end{code}
\begin{tabb} Returns a string starting with \texttt{"Baker transformation of "}
  and finishing with the result of the call to the \texttt{toString}
  method of the generator.
\end{tabb}
\begin{code}

   public double nextDouble() \begin{hide} {
      double u = st.nextDouble();
      if (u > 0.5) return 2.0 * (1.0 - u);
      else return u + u;
   }\end{hide}
\end{code}
\begin{tabb} Returns the baker transformation of \texttt{s.nextDouble()} 
  where \texttt{s} is the base stream.
\end{tabb}
\begin{code}

   public int nextInt (int i, int j) \begin{hide} {
      return i + (int)(nextDouble() * (j - i + 1.0));
   }\end{hide}
\end{code} 
\begin{tabb} Generates a random integer in $\{i,...,j\}$ via
 \method{nextDouble}{} (in which the baker transformation is applied).
\end{tabb}
\begin{code}

   public void nextArrayOfDouble (double[] u, int start, int n) \begin{hide} {
       st.nextArrayOfDouble (u, start, n);
       for (int i = start; i < start + n; i++)
          if (u[i] > 0.5) u[i] = 2.0 * (1.0 - u[i]);
          else u[i] += u[i];
       }\end{hide}
\end{code} 
\begin{tabb} Calls \texttt{nextArrayOfDouble (u, start, n)} for the base stream,
  then applies the baker transformation.
\end{tabb}
\begin{htmlonly}
  \param{u}{the array in which the numbers will be stored}
  \param{start}{the first index of \texttt{u} to be used}
  \param{n}{the number of random numbers to put in \texttt{u}}
\end{htmlonly}
\begin{code}

   public void nextArrayOfInt (int i, int j, int[] u, int start, int n) \begin{hide} {
      for(int ii = start; ii < start + n; ii++)
         u[ii] = nextInt(i,j);
   }\end{hide}
\end{code} 
\begin{tabb} Fills up the array by calling \texttt{nextInt (i, j)}.
\end{tabb}
\begin{htmlonly}
  \param{i}{the smallest possible integer to put in \texttt{u}}
  \param{j}{the largest possible integer to put in \texttt{u}}
  \param{u}{the array in which the numbers will be stored}
  \param{start}{the first index of \texttt{u} to be used}
  \param{n}{the number of random numbers to put in \texttt{u}}
\end{htmlonly}
\begin{code}
\begin{hide}
}
\end{hide}
\end{code}
